package com.dmxh.service.impl;

import com.dmxh.mapper.TbSpecificationMapper;
import com.dmxh.mapper.TbSpecificationOptionMapper;
import com.dmxh.params.QueryParams;
import com.dmxh.pojo.TbSpecification;
import com.dmxh.pojo.TbSpecificationExample;
import com.dmxh.pojo.TbSpecificationOption;
import com.dmxh.pojo.TbSpecificationOptionExample;
import com.dmxh.result.PageList;
import com.dmxh.service.SpecService;
import com.dmxh.vo.BrandVo;
import com.dmxh.vo.SpecificationOptionVo;
import com.dmxh.vo.SpecificationVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SpecServiceImpl implements SpecService {

    @Autowired
    private TbSpecificationMapper tbSpecificationMapper;
    @Autowired
    private TbSpecificationOptionMapper tbSpecificationOptionMapper;


    @Override
    public PageList<SpecificationVo> queryPage(QueryParams<SpecificationVo> queryParams) {
        Page<TbSpecification> page = PageHelper.startPage(queryParams.getPageNum(), queryParams.getPageSize());
        TbSpecificationExample tbSpecificationExample = new TbSpecificationExample();
        String specName = queryParams.getTrimParam().getSpecName();
        if (specName != null && !specName.equals("")) {
            tbSpecificationExample.createCriteria().andSpecNameLike("%"+specName+"%");
        }
        List<TbSpecification> tbSpecifications = tbSpecificationMapper.selectByExample(tbSpecificationExample);
        PageInfo<TbSpecification> info = page.toPageInfo();
        List<SpecificationVo> collect = tbSpecifications.stream().map(tbSpecification -> {
            SpecificationVo specificationVo = new SpecificationVo();
            BeanUtils.copyProperties(tbSpecification, specificationVo);
            return specificationVo;
        }).collect(Collectors.toList());
        return new PageList<>(info.getPageNum(), info.getPageSize(), info.getTotal(), collect);
    }

    @Override
    @Transactional
    public void deleteInfo(Integer id) {
        tbSpecificationMapper.deleteByPrimaryKey(id.longValue());
        TbSpecificationOptionExample tbSpecificationOptionExample = new TbSpecificationOptionExample();
        tbSpecificationOptionExample.createCriteria().andSpecIdEqualTo(id.longValue());
        tbSpecificationOptionMapper.deleteByExample(tbSpecificationOptionExample);
    }

    @Override
    public SpecificationVo queryById(long id) {
        TbSpecificationOptionExample tbSpecificationOptionExample = new TbSpecificationOptionExample();
        tbSpecificationOptionExample.createCriteria().andSpecIdEqualTo(id);
        List<TbSpecificationOption> tbSpecificationOptions = tbSpecificationOptionMapper.selectByExample(tbSpecificationOptionExample);
        List<SpecificationOptionVo> collect = tbSpecificationOptions.stream().map(tbSpecificationOption -> {
            SpecificationOptionVo specificationOptionVo = new SpecificationOptionVo();
            BeanUtils.copyProperties(tbSpecificationOption, specificationOptionVo);
            return specificationOptionVo;
        }).collect(Collectors.toList());
        TbSpecification tbSpecification = tbSpecificationMapper.selectByPrimaryKey(id);
        SpecificationVo specificationVo = new SpecificationVo();
        BeanUtils.copyProperties(tbSpecification, specificationVo);
        specificationVo.setOptionVos(collect);
        return specificationVo;
    }

    @Override
    @Transactional
    public void addInfo(SpecificationVo specificationVo) {
        //增加第一个表
        TbSpecification tbSpecification = new TbSpecification();
        BeanUtils.copyProperties(specificationVo,tbSpecification);
        tbSpecificationMapper.insertForKey(tbSpecification);
        //增加第二个表
        List<SpecificationOptionVo> optionVos = specificationVo.getOptionVos();
        List<TbSpecificationOption> collect = optionVos.stream().map(optionVo -> {
            TbSpecificationOption tbSpecificationOption = new TbSpecificationOption();
            BeanUtils.copyProperties(optionVo, tbSpecificationOption);
            tbSpecificationOption.setSpecId(tbSpecification.getId());
            return tbSpecificationOption;
        }).collect(Collectors.toList());
            tbSpecificationOptionMapper.insertBatch(collect);
    }

    @Override
    public void updateInfo(SpecificationVo specificationVo) {
        //修改第一个表
        TbSpecification tbSpecification = new TbSpecification();
        BeanUtils.copyProperties(specificationVo, tbSpecification);
        tbSpecificationMapper.updateByPrimaryKeySelective(tbSpecification);
        //修改第二个表
        TbSpecificationOptionExample tbSpecificationOptionExample = new TbSpecificationOptionExample();
        tbSpecificationOptionExample.createCriteria().andSpecIdEqualTo(tbSpecification.getId());
        tbSpecificationOptionMapper.deleteByExample(tbSpecificationOptionExample);
        List<SpecificationOptionVo> optionVos = specificationVo.getOptionVos();
        List<TbSpecificationOption> collect = optionVos.stream().map(optionVo -> {
            TbSpecificationOption tbSpecificationOption = new TbSpecificationOption();
            BeanUtils.copyProperties(optionVo, tbSpecificationOption);
            tbSpecificationOption.setSpecId(tbSpecification.getId());
            return tbSpecificationOption;
        }).collect(Collectors.toList());
        tbSpecificationOptionMapper.insertBatch(collect);
    }

    @Override
    public void deleteBatch(Integer[] ids) {
        //删除第一张表
        List<Integer> list = Arrays.asList(ids);
        List<Long> collect = list.stream().map(Integer::longValue).collect(Collectors.toList());
        TbSpecificationExample tbSpecificationExample = new TbSpecificationExample();
        tbSpecificationExample.createCriteria().andIdIn(collect);
        tbSpecificationMapper.deleteByExample(tbSpecificationExample);
        //删除第二张表
        TbSpecificationOptionExample tbSpecificationOptionExample = new TbSpecificationOptionExample();
        tbSpecificationOptionExample.createCriteria().andSpecIdIn(collect);
        tbSpecificationOptionMapper.deleteByExample(tbSpecificationOptionExample);
    }

    @Override
    public List<SpecificationVo> queryAllSpec() {
        List<SpecificationVo> collect = tbSpecificationMapper.selectByExample(null).stream().map(tbSpecification -> {
            SpecificationVo specificationVo = new SpecificationVo();
            BeanUtils.copyProperties(tbSpecification, specificationVo);
            return specificationVo;
        }).collect(Collectors.toList());
        return collect;
    }
}
